﻿using UnityEngine;
using System.Collections;

public class ResolutionGUI : MonoBehaviour 
{
    //Resolution native de la gui
    const int WIDTH = 1920;
    const int HEIGHT = 1080;
    public Texture2D white;

    //-----------------------------------------
    //
    //-----------------------------------------
    public void DrawBandesNoires()
    {
        float sw = Screen.width;
        float sh = Screen.height;
        float refw = WIDTH;
        float refh = HEIGHT;
        float rCurrentRatio = sw / sh;
        float guiScale;
        if (rCurrentRatio > (refw / refh))
        {
            //Bandes Droite/Gauche
            guiScale = sh / refh;
        }
        else
        {
            //Bandes Haut/Bas
            guiScale = sw / refw;
        }
        float cw = refw * guiScale;
        float ch = refh * guiScale;
        Vector3 v = new Vector3((sw - cw) / 2, (sh - ch) / 2, 0);

        GUI.matrix = Matrix4x4.identity;
        if (v.x > 0)
        {
            //Bandes sur les cotés
            GUI.color = Color.black;
            GUI.DrawTexture(new Rect(0, 0, v.x, Screen.height), white);
            GUI.DrawTexture(new Rect(Screen.width - v.x, 0, v.x, Screen.height), white);
        }
        if (v.y > 0)
        {
            //Bandes Haut et Bas
            GUI.color = Color.black;
            GUI.DrawTexture(new Rect(0, 0, Screen.width, v.y), white);
            GUI.DrawTexture(new Rect(0, Screen.height - v.y, Screen.width, v.y), white);
        }
        GUI.color = Color.white;

        //Setup matrix for next gui draw
        Vector3 translation = new Vector3(v.x, v.y, 0);
        GUI.matrix = Matrix4x4.TRS(translation, Quaternion.identity, Vector3.one * guiScale);
    }

    
 void OnGUI()
    {
        DrawBandesNoires();
    }
}
